﻿using Mercury.Business.IRepositories;
using Mercury.Business.IServices;
using Mercury.Core.BaseProvider;
using Mercury.Core.CacheManager;
using Mercury.Core.Common;
using Mercury.Core.Controllers.Basic;
using Mercury.Core.Extensions;
using Mercury.Core.Utilities;
using Mercury.Entity.AttributeManager;
using Mercury.Entity.DomainModels;
using Mercury.Entity.SystemModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;

namespace Mercury.TripartiteApi.Controllers.System
{
    [Route("api/Computed")]
    public class ComputedController : ApiBaseController<IBaseEntityService>
    {
        private ICacheService _cache;
        IBaseEntityRepository _repository;
        [ActivatorUtilitiesConstructor]
        public ComputedController(
                      IBaseEntityService service,
                      IBaseEntityRepository repository,
                      ICacheService cahce
                     )
                 : base(service)
        {
            _cache = cahce;
            _repository = repository;
        }


        [Route("GetAmount/{orderNo}"), HttpGet]
        public async Task<WebResponseContent> GetMaterialsAmount(string orderNo)
        {
            var order = await _repository.DbContext.Set<OrderForm>().FirstOrDefaultAsync(c => c.OrderNumber == orderNo);
            if (order == null || string.IsNullOrEmpty(order.ActualHeatNumber))
                return new WebResponseContent
                {
                    Status = false,
                    Message = "计划单异常！"
                };
            var heat = await _repository.DbContext.Set<Base_InductionFurnace>().FirstOrDefaultAsync(c => c.HeatNo == order.ActualHeatNumber);
            if (heat == null)
                return new WebResponseContent
                {
                    Status = false,
                    Message = "感应炉信息异常！"
                };
            var weight = heat.SetYield.GetDouble();
            if (weight < 0 )
                return new WebResponseContent
                {
                    Status = false,
                    Message = "感应炉信息异常！"
                };
            var result = await order.SteelGrade.MaterialCalculation(orderNo, weight, _repository);
            return new WebResponseContent()
            {
                Status = result.Item1,
                Message = result.Item2,
                Data = result.Item3
            };

        }
    }
}
